package com.yyw.foodie.controller;

import com.yyw.foodie.common.Result;
import com.yyw.foodie.constant.BaseConstant;
import com.yyw.foodie.enums.YesOrNoEnums;
import com.yyw.foodie.pojo.CarouselEntity;
import com.yyw.foodie.pojo.CategoryEntity;
import com.yyw.foodie.service.CarouselService;
import com.yyw.foodie.service.CategoryService;
import com.yyw.foodie.utils.JsonUtils;
import com.yyw.foodie.utils.RedisOperator;
import com.yyw.foodie.vo.CategoryTwoVo;
import com.yyw.foodie.vo.NewSixCategoryVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author 易煜玮
 * @ClassName TestController.java
 * @Description
 * @createTime 2021年05月22日 10:53:00
 */
@Api(value = "首页相关接口", tags = "首页相关接口")
@RestController
@RequestMapping("/index")
public class IndexController {

    @Autowired
    private CarouselService carouselService;

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private RedisOperator redisOperator;

    @ApiOperation(value = "获取轮播图数据", notes = "首页加载获取轮播图数据")
    @GetMapping("/carousel")
    public Result getCarousel() {
        // 判断Redis是否存在数据
        String value = redisOperator.get(BaseConstant.CAROUSEL);
        List<CarouselEntity> carouselList;
        if (StringUtils.isBlank(value)) {
            carouselList = carouselService.getCarouselList(YesOrNoEnums.是.code);
            // 存放Redis
            redisOperator.set(BaseConstant.CAROUSEL, JsonUtils.objectToJson(carouselList), BaseConstant.CACHE_TIME);
        }else {
            carouselList = JsonUtils.jsonToList(value, CarouselEntity.class);
        }
        return Result.ok(carouselList);
    }

    @ApiOperation(value = "获取一级分类数据", notes = "首页加载后先加载一级分类")
    @GetMapping("/cats")
    public Result cats() {
        String value = redisOperator.get(BaseConstant.CATS1);
        List<CategoryEntity> category;
        if (StringUtils.isBlank(value)) {
            category = categoryService.getOneCategory();
            // 存放Redis
            redisOperator.set(BaseConstant.CATS1, JsonUtils.objectToJson(category), BaseConstant.CACHE_TIME);
        }else {
            category = JsonUtils.jsonToList(value, CategoryEntity.class);
        }
        return Result.ok(category);
    }

    @ApiOperation(value = "获取子类分类数据", notes = "根据父ID查询子分类数据")
    @GetMapping("/subCat/{rootCatId}")
    public Result subCat(@ApiParam(value = "父ID", required = true) @PathVariable Integer rootCatId) {
        if (rootCatId == null) {
            return Result.errorMsg("分类不存在");
        }
        String value = redisOperator.get(BaseConstant.SUBCAT + rootCatId);
        List<CategoryTwoVo> category;
        if (StringUtils.isBlank(value)) {
            category = categoryService.getCategoryByPid(rootCatId);
            // 存放Redis
            redisOperator.set(BaseConstant.SUBCAT + rootCatId, JsonUtils.objectToJson(category), BaseConstant.CACHE_TIME);
        }else {
            category = JsonUtils.jsonToList(value, CategoryTwoVo.class);
        }
        return Result.ok(category);
    }


    @ApiOperation(value = "获取最新的商品", notes = "根据一级分类ID获取最新的6个商品信息")
    @GetMapping("/sixNewItems/{rootCatId}")
    public Result getSixNewItems(@ApiParam(value = "分类父id",required = true)
                                 @PathVariable("rootCatId") Integer rootCatId) {
        List<NewSixCategoryVo> newSixItems = categoryService.getNewSixItems(rootCatId);
        return Result.ok(newSixItems);
    }


}
